﻿var topicName = "ChatTopic"
var userName = null;
var endpoint = "localhost";//74.208.226.12
var isRunning = false;

var ChatTopic = {};

ChatTopic.New = function(username, message){
    return {UserName: username, Message: message};
}

function registerTopic() {
    PSB.register({topic: topicName, info: {UserName: "Name of User", Message: "Message sent by user"} });
}

function registerUser() {
	PSB.subscribe({
        topic: topicName,
		filter: "UserName != " + userName,
        callback: handleMessage
    });
    sendMessage("add-user");
}

function handleMessage(msg) {
    if (msg.Message == null) return;
    if (msg.Message == "add-user")
        printMessage(null, String.format("{0} has joined the chat room", msg.UserName));
    else if (msg.Message == "remove-user")
        printMessage(null, String.format("{0} has left the chat room", msg.UserName));
    else printMessage(msg.UserName, msg.Message);
}

function sendInput() {
    var inputBox = $("#inputBox");
    var value = inputBox.val();
    sendMessage(value);
    printMessage("You", value);
    inputBox.val("");
}

function endChat(elem) {
    shutDown();
	var msgBox = $("#messageBox");
	msgBox.html("");
	msgBox.scrollTop($("#messageBox")[0].scrollHeight);
	$.mobile.changePage($("#loginPage"))
}


function printMessage(username, message) {
    var msgBox = $("#messageBox");
	var userText = username != null ? ("<span class='username'>" + username + ":</span> ") : "";
	msgBox.append("<div class='message'>" + userText + message + "</div>");
	msgBox.scrollTop($("#messageBox")[0].scrollHeight);
}

function shutDown() {
	if(!isRunning) return;
    sendMessage("remove-user");
    PSB.unsubscribe({ topic: topicName });
	isRunning = false;
}

function sendMessage(message) {
    PSB.publish({
        topic: topicName,
        message: ChatTopic.New(userName, message)
    });
}

function setupUserName() {
    userName = $("#userNameInput").val().trim();
    if (userName.length <= 0) {
        alert("UserName length must be greater than zero!");
        return;
    }
    $.mobile.changePage($("#chatPage"))
	$("#messageBox").focus();
    registerUser();
	isRunning = true;
}


$(document).ready(function () {
	PSB.setting.logging = true;
	PSB.setting.clienttoken(new Date().getTime().toString());
	PSB.setting.endpoint(String.format("http://{0}:8087/ESB/", endpoint));
	PSB.setting.transport.address(String.format("endpoint://guest:guest@{0}:5672/", endpoint));
	registerTopic();
    $("#userNameButton").click(function () {
        setupUserName();
    });
    $("#userNameInput").keydown(function (e) {
        code = (e.keyCode ? e.keyCode : e.which);
        if (code == 13) {
            setupUserName();
            e.preventDefault();
        }
    });
    $("#inputBox").keydown(function (e) {
        code = (e.keyCode ? e.keyCode : e.which);
        if (code == 13) {
            sendInput();
            e.preventDefault();
        }
    });
	$("#userNameInput").focus();
	PSB.ondisconnect = function(){
		if (isRunning)
			sendMessage("remove-user");
	}
});